﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace TestNetReports
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
		TestNetReports.TestNetReportsDBDataSetTableAdapters.PeopleTableAdapter testNetReportsDBDataSetPeopleTableAdapter = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TestNetReports.TestNetReportsDBDataSet testNetReportsDBDataSet = ((TestNetReports.TestNetReportsDBDataSet)(this.FindResource("testNetReportsDBDataSet")));
            // Load data into the table People. You can modify this code as needed.
            testNetReportsDBDataSetPeopleTableAdapter = new TestNetReports.TestNetReportsDBDataSetTableAdapters.PeopleTableAdapter();
            testNetReportsDBDataSetPeopleTableAdapter.Fill(testNetReportsDBDataSet.People);
            System.Windows.Data.CollectionViewSource peopleViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("peopleViewSource")));
            peopleViewSource.View.MoveCurrentToFirst();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ////////////////////
            // Генерация отчета
			string pathTmpl = "";
			try
			{
				using (IAZ.netreports.NetReport netReport = new IAZ.netreports.NetReport("Templates\\templates.xml"))
				{
					netReport.SetInitstring(Properties.Settings.Default.TestNetReportsDBConnectionString);
					netReport.SetReport("TestPeople");

					pathTmpl = "output\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
					if (!netReport.Generate("TestPeopleTmpl", ref pathTmpl))
					{
						MessageBox.Show("Отчет не был создан.", "Сообщение", MessageBoxButton.OK);
						return;
					}
				}
			}
			catch (System.IO.FileNotFoundException ex)
			{
				MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK);
				return;
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK);
				return;
			}

            // Открытие отчета
            MessageBoxResult res = MessageBox.Show("Отчет успешно создан. Открыть его?\n(\"" + pathTmpl + "\")",
                "Сообщение", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                using (Process docExcel = new Process())
                {
                    docExcel.StartInfo.FileName = pathTmpl;
                    docExcel.StartInfo.Verb = "Open";
                    docExcel.StartInfo.CreateNoWindow = false;
                    docExcel.Start();
                }
            }
        }

		private void peopleDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
		{
			System.Data.DataRowView dataRow = e.Row.Item as System.Data.DataRowView;

			if (dataRow.IsEdit)
			{
				if (dataRow[0] == System.DBNull.Value)
				{
					if (peopleDataGrid.Items.Count >= 3)
					{
						System.Data.DataRowView dataLastRow = (System.Data.DataRowView)(peopleDataGrid.Items[peopleDataGrid.Items.Count - 3]);
						dataRow[0] = (int)dataLastRow[0] + 1;
					}
					else
						dataRow[0] = 1;
				}
				dataRow.EndEdit();
			}

			if (dataRow != null)
			{
				if (dataRow.Row.RowState == System.Data.DataRowState.Modified)
					testNetReportsDBDataSetPeopleTableAdapter.Update(dataRow.Row);
				else
				if (dataRow.Row.RowState == System.Data.DataRowState.Added)
					testNetReportsDBDataSetPeopleTableAdapter.Insert((String)dataRow.Row["FirstName"], (String)dataRow.Row["LastName"], (String)dataRow.Row["MiddleName"]);
			}
		}
    }
}
